package com.honglang.module.user.action;

import java.io.IOException;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.StringUtil;
import com.honglang.module.log.service.LoginLogService;
import com.honglang.module.user.entity.User;
import com.honglang.module.user.service.UserService;
import com.honglang.struts2.action.BaseAction;
import com.opensymphony.xwork2.Action;

public class LoginAction extends BaseAction{
	
	private User user;
	private UserService userService;
	private LoginLogService loginLogService;
	
	
	public String adminLogin() throws IOException{
		user.setUserName(this.getRequest().getParameter("username"));
		user.setPassword(this.getRequest().getParameter("password"));
		if(StringUtil.isBlank(user.getUserName())||StringUtil.isBlank(user.getPassword())){
			return Action.LOGIN;
		}
		user.setRole(Constants.Administrator);
		if(userService.isUserExist(user)){
			this.getSession().setAttribute(Constants.USERNAME_KEY, user.getUserName());
			this.getSession().setAttribute(Constants.USER_KEY, this.getUserService().getUserByUserName(user.getUserName()));
			this.getSession().setAttribute(Constants.Role, Constants.Administrator);
			this.getLoginLogService().saveLoginLog(this.getUserService().getUserByUserName(user.getUserName()));
			this.getResponse().getWriter().write("true");
			return Action.NONE;
		}else{
			this.getResponse().getWriter().write("false");
			return Action.NONE;
		}
		
	}
	
	
	

	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}



	public LoginLogService getLoginLogService() {
		return loginLogService;
	}



	public void setLoginLogService(LoginLogService loginLogService) {
		this.loginLogService = loginLogService;
	}
	
	
	
}
